let eye1 = document.getElementById('eye1');
let password = document.getElementById('password');
let flag1 = 0;
eye1.onclick = function(){
    if (flag1 == 0){
        password.type = 'text';
        eye1.src='imgopen.jpg';
        flag1 = 1;
    }else{
        password.type = 'password';
        eye1.src='imgclose.jpg';
        flag1 = 0;
    }
}
let eye2 = document.getElementById('eye2');
let password2 = document.getElementById('password2');
let flag2 = 0;
eye2.onclick = function(){
    if (flag2 == 0){
        password2.type = 'text';
        eye2.src='imgopen.jpg';
        flag2 = 1;
    }else{
        password2.type = 'password';
        eye2.src='imgclose.jpg';
        flag2 = 0;
    }
}







window.onload = function(){
    document.getElementById("form").onsubmit = function(event){
        event.preventDefault();
        if(checkUsername() && checkPassword() && checkPassword2() && mailbox()){
            window.location.replace('index.html');
        }else{
            alert("Form submisson failed");
        }
    }
        document.getElementById("username").onblur = checkUsername;
        document.getElementById("password").onblur = checkPassword;
        document.getElementById("password2").onblur = checkPassword2;
        document.getElementById("email").onblur = mailbox;      
    } 
    
    function checkUsername(){
        let username = document.getElementById("username").value;
        let reg_username = /^[a-zA-Z0-9]+$/;
        let flag = reg_username.test(username);
        let s_username = document.getElementById("s_username");
        if(flag){
            s_username.innerHTML = "<img width='35' height='25' src='../imgs/right.png'/>";
            return true;
        }else{
            s_username.innerHTML = "Format error";
            return false;
        }
                
    }
            
    function checkPassword(){
        let password = document.getElementById("password").value;
        let reg_password = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,10}$/;
        let flag = reg_password.test(password);
        let s_password = document.getElementById("s_password");
        if(flag){
            s_password.innerHTML = "<img width='35' height='25' src='../imgs/right.png'/>";
            return true;
        }else{
            s_password.innerHTML = "Format error";
            return false;
        }
    }
    
    function checkPassword2(){
        let password2 = document.getElementById("password2").value;
        let reg_password2 = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,10}$/;
        let flag = reg_password2.test(password2);
        let s_password2 = document.getElementById("s_password2");
        if(flag && password2 == document.getElementById("password").value){
            s_password2.innerHTML = "<img width='35' height='25' src='../imgs/right.png'/>";
            return true;
        }else{
            s_password2.innerHTML = "Inconsistent format";
            return false;
        }
    }
    
    function mailbox(){
    let email =document.getElementById("email").value; 
    let emailReg=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    let flag = emailReg.test(email);
    let test_email = document.getElementById("test_email");
        if(flag){
            test_email.innerHTML = "<img width='35' height='25' src='../imgs/right.png'/>";
            return true;
        }else{
            test_email.innerHTML = "Format error";
            return false;
        }
    }  